<template>
  <div>
  <el-row>
    <el-col :span="22" :offset="1">
      <el-button type="primary" @click="add">添加</el-button>
      <el-table
        :data="tableData"
        highlight-current-row
        ref="tab"
        @current-change="handleCurrentChange"
        border
        style="width: 100%">
        <el-table-column
          fixed
          prop="id"
          label="ID">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色名">
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row.id,scope.row.name,scope.row.remark)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id,scope.row.name)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider content-position="left">角色菜单授权</el-divider>
      <el-tree
        :data="menus"
        :props="props"
        node-key="id"
        :show-checkbox="!bool"
        ref="tree"
        @check="(click, checked)=>{handleCheckChange(click, checked)}">
        <!--  插槽，放自定义节点内容 -->
        <!-- 此处的class和slot-scope均是固定写法 -->
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <div style="line-height: 2.15; display :flex">
            <div style="width:150px">{{ node.label }}</div>
            <div style="width:150px" v-if="data.type==0"><el-tag type="success">目录</el-tag></div>
            <div style="width:150px" v-else-if="data.type==1"><el-tag type="warning">菜单</el-tag></div>
            <div style="width:150px" v-else><el-tag type="success">按钮</el-tag></div>
            <div style="width:150px"><i v-if="data.icon!=null" :class="data.icon"></i></div>
            <div style="width:150px">{{ data.parentId == 0 ? '顶级菜单':''}}</div>
            <div style="width:150px">{{ data.url}}</div>
          </div>
        </span>
      </el-tree>
      <div>
        <el-col :span="6">
          <el-checkbox v-model="checkAll" @change="handleCheckAllChange" :disabled="bool" style="line-height: 36px;">全选</el-checkbox>
        </el-col>
        <el-col :span="4" :offset="14">
          <el-button type="danger" :disabled="bool" @click="reset">重置</el-button>
          <el-button type="success" :disabled="bool" @click="submit">提交</el-button>
        </el-col>
      </div>
    </el-col>
    <edit-role ref="edit" @refresh="getRole"></edit-role>
    <add-role ref="add" @refresh="getRole"></add-role>
  </el-row>
</div>
</template>

<script>
import AddRole from './AddRole.vue'
import EditRole from './EditRole.vue'
export default {
  components: { EditRole, AddRole },
  data(){
    return{
      tableData:[],
      props: {
          id:'id',
          label: 'title',
          children: 'children'
      },
      menus:[],
      bool:true,
      checkAll: false,
      selectId:[],
      roleId:''
    }
  },
  mounted(){
    this.getRole()
  },
  methods:{
    edit(id,name,remark){
      this.$refs.edit.$data.role = {
        id:id,
        name:name,
        remark:remark
      }
      this.$refs.edit.$data.iseditRoleDialog=true
    },
    async getRole(){
      const res = await this.$http.get('/role')
      this.tableData = res.data.data
      const res2 = await this.$http.get('/menu/multistageMenus')
      this.menus = res2.data.data
    },
    add(){
      this.$refs.add.$data.iseditRoleDialog2=true
    },
    async del(id,name){
      try {
        await this.$confirm(`确认要删除${name}吗?`, '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        const res = await this.$http.delete(`/role/${id}`)
        if(res.data.code == 0){
          this.$message({
              type: 'success',
              message: '删除成功!'
          });
          this.getRole()
        }
      } catch (err) {
        this.$message({
            type: 'info',
            message: '已取消删除'
        });  
      }
    },
    // 获取选择的节点数组
    handleCheckChange(click, checked){
      this.selectId = checked.checkedKeys
    },

    //表格单选，获得对应的菜单
    handleCurrentChange(val) {
      this.selectId=[]
      this.$refs.tree.setCheckedKeys([]);
      this.roleId = val.id
      this.$http.get(`/role/${val.id}`).then(res => {
        this.$refs.tree.setCheckedKeys(res.data.data);
        this.selectId=res.data.data
        this.bool=false
      })
      return
    },

    //重置
    reset(){
      this.$refs.tree.setCheckedKeys([]);
      this.bool=true
    },

    //全选
    handleCheckAllChange(val) {
      if (val) {
          this.$refs.tree.setCheckedNodes(this.menus);
          this.selectId=this.$refs.tree.getCheckedKeys();
          // this.$http.get(`/role/all`).then(res =>{
          //    this.selectId=res.data.data
          // })
      } else {
          this.$refs.tree.setCheckedKeys([]);
          this.selectId=[]
      }
    },

    //提交
    submit(){
      let data = {
        id : this.roleId,
        roleList : this.selectId
      }
      this.$http.put('role/menu',data,{indices:false,headers:{'Content-Type':'application/json'}}).then(res=>{
          if(res.data.code ==0){
            this.$refs.tab.clearSelection()
            this.$message({
            type: 'success',
            message: '修改成功!',
            });
            this.getRole()
            this.bool=true
            this.$refs.tree.setCheckedKeys([]);
          }else{
              this.$message.error('修改失败');
          }
      })
    }
  }
}
</script>

<style>

</style>